package model

import (
	"gorm.io/gorm"
)

type UserClient struct {
	db *gorm.DB
}

func NewUserClient(db *gorm.DB) *UserClient {
	return &UserClient{db: db}
}

// 创建用户
func (uc *UserClient) CreateUser(user *User) error {
	return uc.db.Create(user).Error
}

// 查找用户条件
type UserCondition struct {
	ID        string   `json:"id"`
	Username  string   `json:"username"`
	Password  string   `json:"password"`
	Nickname  string   `json:"nickname"`
	Email     string   `json:"email"`
	Phone     string   `json:"phone"`
	CreatedAt string   `json:"created_at"`
	TenantID  string   `json:"tenant_id"`
	IDs       []string `json:"ids"`
}

func (uc *UserClient) FindUserByCondition(condition *UserCondition) (*User, error) {
	// 构建query
	query := uc.db.Model(&User{})
	// 根据条件查找用户
	if condition.ID != "" {
		query = query.Where("id = ?", condition.ID)
	}
	if condition.Username != "" {
		query = query.Where("username = ?", condition.Username)
	}
	if condition.Password != "" {
		query = query.Where("password = ?", condition.Password)
	}
	if condition.Nickname != "" {
		query = query.Where("nickname LIKE ?", "%"+condition.Nickname+"%")
	}
	if condition.Email != "" {
		query = query.Where("email LIKE ?", "%"+condition.Email+"%")
	}
	if condition.Phone != "" {
		query = query.Where("phone = ?", condition.Phone)
	}
	if condition.CreatedAt != "" {
		query = query.Where("created_at >= ?", condition.CreatedAt)
	}

	// 执行查询
	var user User
	err := query.First(&user).Error
	return &user, err
}

// 查找用户列表
func (uc *UserClient) BatchFindUserByCondition(condition *UserCondition, page, size int) ([]*User, error) {
	// 构建query
	query := uc.db.Model(&User{})
	// 根据条件查找用户
	if condition.Username != "" {
		query = query.Where("username = ?", condition.Username)
	}
	if condition.Password != "" {
		query = query.Where("password = ?", condition.Password)
	}
	if condition.Nickname != "" {
		query = query.Where("nickname LIKE ?", "%"+condition.Nickname+"%")
	}
	if condition.Email != "" {
		query = query.Where("email LIKE ?", "%"+condition.Email+"%")
	}
	if condition.Phone != "" {
		query = query.Where("phone = ?", condition.Phone)
	}
	if condition.CreatedAt != "" {
		query = query.Where("created_at >= ?", condition.CreatedAt)
	}
	if condition.TenantID != "" {
		query = query.Where("tenant_id = ?", condition.TenantID)
	}
	if len(condition.IDs) > 0 {
		query = query.Where("id IN (?)", condition.IDs)
	}

	// 分页查询
	query = query.Offset((page - 1) * size).Limit(size).Order("created_at desc")

	// 执行查询
	var user []*User
	err := query.Find(&user).Error
	return user, err
}

// 更新用户
func (uc *UserClient) UpdateUser(user *User) error {
	data := map[string]interface{}{}

	if user.Password != "" {
		data["password"] = user.Password
	}
	if user.Nickname != "" {
		data["nickname"] = user.Nickname
	}
	if user.AvatarID != "" {
		data["avatar_id"] = user.AvatarID
	}
	if user.Email != "" {
		data["email"] = user.Email
	}
	if user.Phone != "" {
		data["phone"] = user.Phone
	}

	return uc.db.Model(&User{}).Where("id = ?", user.ID).Updates(data).Error
}

// 根据id查询用户是否是管理员
func (uc *UserClient) IsAdminByID(id string) (bool, error) {
	var user User
	err := uc.db.Model(&User{}).Where("id = ?", id).First(&user).Error
	if err != nil {
		return false, err
	}
	if user.IsSuper == 1 {
		return true, nil
	}
	return false, nil
}
